package com.car.account.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.car.account.dto.storesResource.StoresResourceDto;
import com.car.account.entity.*;
import com.car.account.exception.AccountException;
import com.car.account.mapper.*;
import com.car.account.service.StoresResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 董旭
 * @since 2024-08-28
 */
@Service
public class StoresResourceServiceImpl extends ServiceImpl<StoresResourceMapper, StoresResource> implements StoresResourceService {

    @Resource
    private StoresMapper storesMapper;
    @Resource
    private ClerRoleMapper clerRoleMapper;
    @Resource
    private StoresPermissionMapper storesPermissionMapper;
    @Resource
    private StoresResourceMapper storesResourceMapper;
    
    
    @Override
    public ArrayList<StoresResourceDto> load(Integer clerkId) {
        Stores clerk = storesMapper.selectById(clerkId);
        if (clerk == null) {
            throw new AccountException(500, "该门店管理员不存在");
        }
        if (!clerk.getClerkState().equals(0)) {
            throw new AccountException(500, "门店管理员状态异常");
        }
        QueryWrapper<ClerRole> qw = new QueryWrapper<>();
        qw.eq("clerk_id", clerk.getClerkId());
        ClerRole clerRole = clerRoleMapper.selectOne(qw);
        if (clerRole == null) {
            throw new AccountException(500, "该门店管理员角色异常");
        }
        QueryWrapper<StoresPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role", clerRole.getRoleId());
        List<StoresPermission> storesPermissions = storesPermissionMapper.selectList(queryWrapper);
        List<Integer> collect = storesPermissions.stream().map(o -> o.getResourceId()).collect(Collectors.toList());
        List<StoresResource> storesResources = storesResourceMapper.selectBatchIds(collect);
        ArrayList<StoresResourceDto> l1 = new ArrayList<>();
        for (StoresResource storesResource : storesResources) {
            if (storesResource.getResourceLevel().equals(1)) {
                StoresResourceDto platformResourceDto = BeanUtil.toBean(storesResource, StoresResourceDto.class);
                l1.add(platformResourceDto);
            }
        }
        for (StoresResource storesResource : storesResources) {
            if (storesResource.getResourceLevel().equals(2)) {
                for (StoresResourceDto resourceDto : l1) {
                    if (resourceDto.getResourceId().equals(storesResource.getParentId())) {
                        StoresResourceDto storesResourceDto = BeanUtil.toBean(storesResource, StoresResourceDto.class);
                        resourceDto.getChildren().add(storesResourceDto);
                    }
                }
            }
        }
        return l1;
    }
}
